<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col :span="20">
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    // 1. 接收外部传递的数据对象
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 1. 处理下拉菜单的点击
    handleCommand(command) {
      // 1.1 添加
      if (command === 'add') {
        this.$emit('addDept', this.treeNode)
      }

      // 1.2 编辑
      if (command === 'edit') {
        this.$emit('editDept', this.treeNode)
      }

      // 1.3 删除
      if (command === 'del') {
        // 1.3.1 提示用户是否删除
        this.$confirm('您确定要删除该部门吗?', '温馨提示', { type: 'warning' }).then(async() => {
          // 1.3.2 调用删除接口
          await delDepartments(this.treeNode.id)
          // 1.3.3 提示删除成功
          this.$message.success('成功删除部门')
          // 1.3.4 通知父组件更新列表数据
          this.$emit('delDept')
        }).catch(() => {

        })
      }
    }
  }
}
</script>

<style>

</style>
